﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestTasks.ThreadConsistency
{
    public class ConsistencyAccount : IAccount
    {
        public ConsistencyAccount(int number, int amount) 
        {
            this.number = number;
            this.amount = amount;
        }

        readonly int number;
        public int Number
        {
            get { lock (this) { return number; } }
        }

        int amount;
        public int Amount
        {
            get { lock (this) { return amount; } }
        }

        public bool Withdraw(int amount)
        {
            lock (this)
            {
                if (this.amount < amount) return false;
                this.amount -= amount;
                return true;
            }
        }

        public void Deposit(int amount)
        {
            lock (this) { this.amount += amount; }
        }

        public int CompareTo(IAccount other)
        {
            var brother = other as ConsistencyAccount;
            
            if (brother == null) throw new ArgumentException();

            return number.CompareTo(brother.number);
        }
    }
}
